package Leetcode第一期;

import java.util.*;

/**
 * @author : K k
 * @date : 10:14 2020/9/3
 */
public class 从上到下打印二叉树_31_1 {
    public int[] levelOrder(TreeNode root) {
        if (root==null){
            return new int[0];
        }
        Queue<TreeNode> queue = new LinkedList<>();

        List<Integer> list=new ArrayList<>();

        queue.add(root);
        while (!queue.isEmpty()){
            //出队
            TreeNode node = queue.poll();
            list.add(node.val);
            if (node.left!=null){
                queue.add(node.left);
            }
            if (node.right!=null){
                queue.add(node.right);
            }
        }

        int[] res = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            res[i] = list.get(i);
        }
        return res;
    }

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
}

